/**
 * Created by zhourb on 2017/12/17.
 */
var $ = require('jquery');
require('easyui');
require('layout/uiJs');
var $alert = require("libs/alert");
var $confirm = require("libs/confirm");
var isActive={"Y":"有效","N":"<font color='red'>无效</font>"};
var clickEvent = {
    modifyInit:function () {
        $("body").on("click","a[name=modify]",function () {
            var id = $(this).parents("tr").children(":first").text();
            window.location.href="promotion-add?id="+id;
        });
        return clickEvent;
    },
    activeInit:function () {
        $("body").on("click","a[name=active]",function () {
            var id = $(this).parents("tr").children(":first").text();
            $confirm("确认启用该用户吗?",function () {
                $.ajax({
                    url:"api/v1/p/mms/promotion/updateStatus",
                    type:"post",
                    dataType:"json",
                    contentType: "application/json; charset=utf-8",
                    data:JSON.stringify({
                        "id":id,
                        "status":"Y"
                    }),
                    success:function (result) {
                        if(result.code=="200") {
                            $alert("启用成功!");
                            $("#main_table").datagrid("reload");
                        }else{
                            $alert(result.message);
                        }
                    }
                })
            })
        });
        return clickEvent;
    },
    unActiveInit:function () {
        $("body").on("click","a[name=unActive]",function () {
            var id = $(this).parents("tr").children(":first").text();
            $confirm("确认禁用该用户吗?",function () {
                $.ajax({
                    url:"api/v1/p/mms/promotion/updateStatus",
                    type:"post",
                    dataType:"json",
                    contentType: "application/json; charset=utf-8",
                    data:JSON.stringify({
                        "id":id,
                        "status":"N"
                    }),
                    success:function (result) {
                        if(result.code=="200") {
                            $alert("禁用成功!");
                            $("#main_table").datagrid("reload");
                        }else{
                            $alert(result.message);
                        }
                    }
                })
            })
        });
        return clickEvent;
    },
    searchInit:function () {
        $("#promotion_search").on("click",function () {
            $("#main_table").datagrid('load', {
                sts: $('#select_status').datebox('getValue'),
                code: $('#tb input[name=code]').val(),
                userName: $('#tb input[name=userName]').val(),
                name: $('#tb input[name=name]').val(),
                dateFrom: $('#startDt').data('utc0'),
                dateTo: $('#endDt').data('utc0')
            });
        });
        return clickEvent;
    }
};
$(function(){
    $(document).on('click','a[name=promotionDetail]',function () {
        window.parent.tabOpen($(this).attr('ref'),'推广人详情',true,true);
    });

    pageInit();
    mainTableInit();
});
function pageInit(){
    $("#promotion_create").show();
    clickEvent.modifyInit().activeInit().unActiveInit().searchInit();
}


function mainTableInit(){
    $("#main_table").datagrid({
        url:'api/v1/p/mms/promotion/list',
        border : false,
        height:$(window).height(),
        pagination : true,
        idField : 'id',
        pageSize : 20,
        pageList : [ 20, 50,  100 ],
        checkOnSelect : true,//单击选中
        emptyMsg : "暂无数据",
        selectOnCheck : true,
        singleSelect : true,
        rownumbers:true,
        columns:[[
            {
                field : 'id',
                hidden:true
            },{
                field:'code',
                title:'推广代码',
                width:'8%',
                formatter:function (value,row,index) {
                    // return "<a href='javascript:;' name='code'>"+value+"</a>"
                    return "<a class='ellipsis' href='javascript:;' ref='promotion-detail?id="+row.id+"' name='promotionDetail' title='"+value+"'>"+value+"</a>";
                }
            },{
                field:'name',
                title:'姓名',
                width:'10%'
            },{
                field:'phone',
                title:'联系电话',
                width:'10%'
            },{
                field:'url',
                title:'推广链接',
                width:'30%'
            },{
                field:'userName',
                title:'关联用户',
                width:'10%'
            },{
                field:'createdDt',
                title:'创建时间',
                width:'13%',
                formatter:function (value,row,index) {
                    return "<div class='ellipsis' title='"+value+"'>"+value+"</div>";
                }
            },{
                field:'nums',
                title:'注册用户数',
                width:'7%'
            },{
                field:'sts',
                title:'状态',
                width:'6%',
                formatter:function (value,row,index) {
                    return isActive[value];
                }
            },{
                field:'opertation',
                title:'操作',
                width:'8%',
                formatter:function (value,row,index) {
                    if(row.sts=="Y"){
                        return "<a href='javascript:;' name='modify'>修改</a>|<a href='javascript:;' name='unActive'>禁用</a>";
                    }else{
                        return "<a href='javascript:;' name='modify'>修改</a>|<a href='javascript:;' name='active'>启用</a>";
                    }
                }
            }
        ]],
        loadFilter:function(data){
            if(data.code!='200'){
                return data.data;
            }
            return data.data;
        },
        onLoadSuccess:function(data){

        },
        toolbar:"#tb"
    })
}